using UnityEngine;
using System.IO;
using UnityEditor;
using Sirenix.OdinInspector;
using System.Text;

public class SystemMgrEditor : EditorWindow
{
    //namespace
    public string ns = "";
    public string systemName = "";
    // string systemPath = "";
    public bool extendMono = false;

    void OnEnable()
    {
        // systemPath = Application.dataPath + "/Scripts/Manager";
    }

    void OnGUI()
    {
        // draw layout
        GUILayout.BeginVertical();

        GUILayout.BeginHorizontal();
        GUILayout.Label("输入命名空间");
        ns = EditorGUILayout.TextField(ns);
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        GUILayout.Label("输入系统名字，不用mgr结尾");
        systemName = EditorGUILayout.TextField(systemName);
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        GUILayout.Label("是否继承SingletonMono");
        extendMono = EditorGUILayout.Toggle(extendMono);
        GUILayout.EndHorizontal();

        if(GUILayout.Button("生成系统模块"))
        {
            // Debug.Log(ns);
            // Debug.Log(systemName);
            if(string.IsNullOrEmpty(systemName))
            {
                Debug.Log("系统名字不能为空");
                return;
            }
            var className = systemName.Trim() + "Mgr";
            if(className.Contains(" "))
            {
                Debug.Log("系统名字不能包含空格");
                return;
            }
            var nameSpace = ns.Trim();
            if(nameSpace.Contains(" "))
            {
                Debug.Log("命名空间不能包含空格");
                return;
            }

            bool validate = Validator.Instance.ValidateType(className,nameSpace);
            if(validate)
            {
                Debug.Log("已经存在该系统模块，请重新输入");
                return;
            }
            Debug.Log("开始生成系统模块");
            GenteratorSystemFile(className,nameSpace);
        }

        GUILayout.EndVertical();
    }

    public void GenteratorSystemFile(string systemName,string nameSpace = "")
    {
        var pathMgr = EditorPathMgr.Instance;
        var systemPath = nameSpace == "" ? pathMgr.SystemModulePath : pathMgr.GetSystemModulePathWithNamespace(nameSpace);
        if(!Directory.Exists(systemPath))
        {
            Directory.CreateDirectory(systemPath);
        }
        string filePath = systemPath + "/" + systemName + ".cs";
        // Debug.Log(filePath);
        if(File.Exists(filePath))
        {
            Debug.Log("已经存在该系统模块，请重新输入");
            // BootGenerator.Instance.UpdateSystemModule(nameSpace,systemName);
            return;
        }

        StringBuilder sb = new StringBuilder();
        sb.AppendLine("using UnityEngine;");
        sb.AppendLine("using System.Collections;");
        sb.AppendLine("using System.Collections.Generic;");
        sb.AppendLine();
        if(nameSpace != "")
        {
            sb.AppendLine("namespace " + nameSpace);
            sb.AppendLine("{");
        }

        var head_code = sb.ToString();
        sb.Clear();
        var parent = extendMono ? "SingletonMono": "Singleton";
        sb.AppendLine($"public class {systemName} : {parent}<{systemName}>");
        sb.AppendLine("{");
        sb.AppendLine("\tpublic override void Init()");
        sb.AppendLine("\t{");
        sb.AppendLine("\t\tbase.Init();");
        sb.AppendLine("\t}");
        sb.AppendLine("}");
        var class_code = sb.ToString();
        sb.Clear();

        if(nameSpace != "")
        {
            StringReader sr = new StringReader(class_code);
            class_code = "";
            string line;
            while((line = sr.ReadLine()) != null)
            {
                class_code += "\t" + line + "\n";
            }
        }

        string sys_code = head_code + class_code;
        if(nameSpace != "")
        {
            sys_code += "}";
        }
        // Debug.Log(sys_code);
        File.WriteAllText(filePath,sys_code);
        BootGenerator.Instance.UpdateSystemModule(nameSpace,systemName);
    }
}